package com.wenhua.controller;

import com.wenhua.mapper.UserMapper;
import com.wenhua.pojo.Dormitory;
import com.wenhua.pojo.DormitoryBuilding;
import com.wenhua.pojo.Result;
import com.wenhua.pojo.User;
import com.wenhua.service.DormitoryService;
import com.wenhua.utils.JwtUtils;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins = "http://localhost:7050")
public class DormitoryController {

    @Autowired
    private  UserMapper userMapper;

    @Autowired
    private DormitoryService dormitoryService;

    //查询宿舍楼
    @GetMapping("/dormitoryBuilding")
    public Result dormitoryBuilding(
            /* 当前页,默认1 */
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            /* 每页多少条数据(默认10) */
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            /* 查询字符串(默认空) */
            @RequestParam(value = "selectString", required = false, defaultValue = "") String selectString,
            /* 排序方式(默认desc) */
            @RequestParam(value = "sort", required = false, defaultValue = "desc") String sort,
            HttpServletRequest request
    ){
        /* 查询宿舍楼 */
        List<DormitoryBuilding> dormitoryBuildings = dormitoryService.selectDormitoryBuilding(currentPage, pageSize, selectString, sort);
        /* 查询宿舍楼总数 */
        Integer dormitoryBuildingCount = dormitoryService.selectDormitoryBuildingCount(selectString);
        System.out.println("查询宿舍楼"+dormitoryBuildingCount);
        System.out.println(dormitoryBuildings);

        //返回
        Map<String ,Object> returnMap = new HashMap<>();
        returnMap.put("dormitoryBuildings",dormitoryBuildings);
        returnMap.put("count",dormitoryBuildingCount);


        return Result.success(returnMap);


    }

    //查询宿舍
    @GetMapping("/dormitory")
    public Result dormitory(
            /* 当前页,默认1 */
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            /* 每页多少条数据(默认10) */
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            /* 查询字符串(默认空) */
            @RequestParam(value = "selectString", required = false, defaultValue = "") String selectString,
            /* 排序方式(默认desc) */
            @RequestParam(value = "sort", required = false, defaultValue = "desc") String sort,
            HttpServletRequest request){
        //查询宿舍
        List<Dormitory> dormitories = dormitoryService.selectDormitory(currentPage, pageSize, selectString, sort);
        //查询宿舍总数
        Integer dormitoryCount = dormitoryService.selectDormitoryCount(selectString);
        System.out.println("查询宿舍"+dormitoryCount);
        System.out.println(dormitories);
        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("dormitories",dormitories);
        returnMap.put("count",dormitoryCount);

        return Result.success(returnMap);
    }

    //通过宿舍楼id查询宿舍方法
    @GetMapping("/dormitoryByBuildingId")
    public Result dormitoryByBuildingId(
            @RequestParam(value = "buildingId", required = true,defaultValue = "1") Integer buildingId){
        //Integer buildingId = building.getBuildingId();
        List<Dormitory> dormitories = dormitoryService.selectDormitoryByBuildingId(buildingId);
        System.out.println(dormitories);
        return Result.success(dormitories);
    }

    /* 直接无条件查询所有宿舍楼 */
    @GetMapping("/dormitoryBuildingAll")
    public Result dormitoryBuildingAll(){
        List<DormitoryBuilding> dormitoryBuildings = dormitoryService.selectDormitoryBuildingAll();
/*        dormitoryBuildings.forEach(d->{
            System.out.println(d.getActualDormitoryCount());
        });*/
        return Result.success(dormitoryBuildings);
    }

    /* 添加宿舍 */
    @PostMapping("/dormitory")
    public Result dormitory(@RequestBody Dormitory dormitory,
            HttpServletRequest request){
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        Integer result = dormitoryService.insertDormitory(dormitory);
        return Result.success(result);
    }

    /* 更新宿舍 */
    @PostMapping("/updateDormitory")
    public Result dormitoryUpdate(@RequestBody Dormitory dormitory,
                                  HttpServletRequest request){
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        Integer result = dormitoryService.updateDormitory(dormitory);
        return Result.success(result);
    }

    /* 添加宿舍楼 */
    @PostMapping("/dormitoryBuilding")
    public Result dormitoryBuilding(
            @RequestBody DormitoryBuilding dormitoryBuilding,
            HttpServletRequest request){
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        System.out.println("添加宿舍楼");
        System.out.println(dormitoryBuilding);
        Integer result = dormitoryService.insertDormitoryBuilding(dormitoryBuilding);
        return Result.success(result);

    }

    /* 修改宿舍楼 */
    @PostMapping("/updateDormitoryBuilding")
    public Result dormitoryBuildingUpdate(
            @RequestBody DormitoryBuilding dormitoryBuilding,
            HttpServletRequest request) {
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        Integer result = dormitoryService.updateDormitoryBuilding(dormitoryBuilding);
        return Result.success(result);
    }

        /* 删除宿舍 */
    @PostMapping("deleteDormitory")
    public Result dormitoryDelete(@RequestBody Dormitory dormitory,
                                  HttpServletRequest request){
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        Integer result = dormitoryService.deleteDormitory(dormitory);
        return Result.success(result);
    }

    /* 删除宿舍楼 */
    @PostMapping("deleteDormitoryBuilding")
    public Result dormitoryBuildingDelete(@RequestBody DormitoryBuilding dormitoryBuilding,
                                          HttpServletRequest request){
        if (!isRoot(request)){
            return Result.error("权限不足");
        }
        Integer result = dormitoryService.deleteDormitoryBuilding(dormitoryBuilding);
        return Result.success(result);
    }

    /* 判断用户是否是管理员方法 */
    public  boolean isRoot(HttpServletRequest request){
        Integer userId = JwtUtils.getUserId(request);
        User user = userMapper.selectUserById(userId);
        System.out.println("用户权位"+user.getUserPermission());
        if(user.getUserPermission()<2){
            return false;
        }
        return true;
    }
}
